﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace JulMar.Siverlight.Helpers.Interactivity
{
    [System.ComponentModel.Description("Launches a Open File Dialog Box on Event Trigger")]
    public class OpenFileDialogBoxBehavior : TargetedTriggerAction<Button>
    {
        #region FileDialogDialogResultCommandProperty

        public static readonly DependencyProperty FileDialogDialogResultCommandProperty =
            DependencyProperty.Register("FileDialogDialogResultCommandProperty",
            typeof(object), typeof(OpenFileDialogBoxBehavior), null);

        public object FileDialogDialogResultCommand
        {
            get
            {
                return (object)base.GetValue(FileDialogDialogResultCommandProperty);
            }
            set
            {
                base.SetValue(FileDialogDialogResultCommandProperty, value);
            }
        }
        #endregion

        #region DialogFilterProperty

        public static readonly DependencyProperty DialogFilterProperty =
           DependencyProperty.Register("DialogFilter", typeof(string),
           typeof(OpenFileDialogBoxBehavior), null);

        public string DialogFilter
        {
            get
            {
                if (base.GetValue(DialogFilterProperty) == null)
                {
                    return "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg|"
                + "PNG Files (*.png)|*.png|All Files (*.*)|*.*";
                }
                else
                {
                    return (string)base.GetValue(DialogFilterProperty);
                }
            }
            set
            {
                base.SetValue(DialogFilterProperty, value);
            }
        }

        #endregion

        #region Invoke

        //Shows OpenFileDialog on event trigger you set in designer

        protected override void Invoke(object parameter)
        {
            OpenFileDialog objOpenFileDialog = new OpenFileDialog();
            objOpenFileDialog.Filter = DialogFilter;

            objOpenFileDialog.ShowDialog();

            FileDialogDialogResultCommand = objOpenFileDialog.File;
        }

        #endregion
    }
}
